package chen.bupt.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

public class SynonymsClean {
	
	//run once
	private static void getSynonymsRoot() throws IOException{
		List<String> list = FileUtils.readLines(new File("data/synonyms6.txt"));
		Map<String, String> map = new HashMap<String, String>();
		Map<String, String> map2 = new HashMap<String, String>();// 指向同义词根

		for (String s : list) {
			String[] tmp = s.split(",");
			if (map.containsKey(tmp[0]) && !map.containsKey(tmp[1])) {
				if (map2.containsKey(tmp[1])) {
					String t1 = map2.get(tmp[1]);
					if(t1.equals(tmp[0]))continue;
					String t2 = map.get(t1);
					map.put(tmp[0], map.get(tmp[0]) + "," + t1 + "," + t2);
					map.remove(t1);
					for (String ss : t2.split(",")) {
						map2.put(ss, tmp[0]);
					}
					map2.put(t1, tmp[0]);
				} else {
					map.put(tmp[0], map.get(tmp[0]) + "," + tmp[1]);
					map2.put(tmp[1], tmp[0]);
				}
			} else if (map.containsKey(tmp[1]) && !map.containsKey(tmp[0])) {
				if (map2.containsKey(tmp[0])) {
					String t1 = map2.get(tmp[0]);
					if(t1.equals(tmp[1]))continue;
					String t2 = map.get(t1);
					map.put(tmp[1], map.get(tmp[1]) + "," + t1 + "," + t2);
					map.remove(t1);
					for (String ss : t2.split(",")) {
						map2.put(ss, tmp[1]);
					}
					map2.put(t1, tmp[1]);
				} else {
					map.put(tmp[1], map.get(tmp[1]) + "," + tmp[0]);
					map2.put(tmp[0], tmp[1]);
				}
			} else if (map.containsKey(tmp[1]) && map.containsKey(tmp[0])) {
				String t1 = map.get(tmp[1]);
				map.remove(tmp[1]);
				map.put(tmp[0], map.get(tmp[0]) + "," + tmp[1] + "," + t1);
				for (String ss : t1.split(",")) {
					map2.put(ss, tmp[0]);
				}
				map2.put(tmp[1], tmp[0]);
			} else {
				if (map2.containsKey(tmp[0]) && !map2.containsKey(tmp[1])) {
					String t = map2.get(tmp[0]);
					map.put(t, map.get(t) + "," + tmp[1]);
					map2.put(tmp[1], t);
				} else if (map2.containsKey(tmp[1])
						&& !map2.containsKey(tmp[0])) {
					String t = map2.get(tmp[1]);
					map.put(t, map.get(t) + "," + tmp[0]);
					map2.put(tmp[0], t);
				} else if (map2.containsKey(tmp[0]) && map2.containsKey(tmp[1])) {
					String k = map2.get(tmp[0]);
					String t1 = map2.get(tmp[1]);
					if(k.equals(t1))continue;
					String t2 = map.get(t1);
					map.put(k, map.get(k) + "," + t1 + "," + t2);
					if(t2==null){
						System.out.println(s);
					}
					for (String ss : t2.split(",")) {
						map2.put(ss, k);
					}
					map2.put(t1, k);
					map.remove(t1);
				} else {
					map.put(tmp[0], tmp[1]);
					map2.put(tmp[1], tmp[0]);
				}
			}
		}

		
		List<String> lines = new ArrayList<String>();
		for (String s : map.keySet()) {
			String t = map.get(s);
			String[] tt = t.split(",");
			for (String ttt : tt) {
				lines.add(s + "," + ttt);
			}
		}
		Collections.sort(lines);
		FileUtils.writeLines(new File("data/synonyms7.txt"), lines);
	}

	//run once
	public void getValiditySynonymsTerm(String srcFile, String dstFile) throws IOException{
		List<String> lines = FileUtils.readLines(new File(srcFile));
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
